import { defineMock } from "../base";

// 局部变量存储定值区信息
let zoneInfo = {
  active_zone: 2,
  min_zone: 1,
  max_zone: 7,
};

export default defineMock([
  // GET uri: /api/setting/{cpu}/zone 查询定值区信息
  {
    url: "setting/:cpu/zone",
    method: ["GET"],
    body({ query, params }) {
      console.log("[MOCK] GET setting/:cpu/zone", query, params);
      return { ...zoneInfo };
    },
  },
  // POST uri: /api/setting/{cpu}/zone 切换定值区
  {
    url: "setting/:cpu/zone",
    method: ["POST"],
    body({ body, params }) {
      console.log("[MOCK] POST setting/:cpu/zone", body, params);
      // 只允许切换到 min~max 区间
      const { active_zone } = body || {};
      if (
        typeof active_zone === "number" &&
        active_zone >= zoneInfo.min_zone &&
        active_zone <= zoneInfo.max_zone
      ) {
        zoneInfo.active_zone = active_zone;
        return { success: true, ...zoneInfo };
      }
      return {
        success: false,
        message: `active_zone 必须在 [${zoneInfo.min_zone}, ${zoneInfo.max_zone}] 区间`,
        ...zoneInfo,
      };
    },
  },
]);
